package com.shop.controller.admin;

import java.sql.SQLException;

import com.jf.core.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.shop.bean.PLiuyanBean;
import com.shop.utils.StringUtils;

public class PLiuyanController extends BaseController{
	/**
	 * 获取所有的评论列表
	 * */
	public void plyList(){
		String select = "select tblply.*,tblp.pname,tblyh.uname";
		String sqlExceptSelect = "from tblpliuyan tblply inner join tblproduct tblp on tblply.pcode=tblp.pcode"+
		" inner join tblyonghu tblyh on tblply.ucode=tblyh.ucode";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<PLiuyanBean> plys = PLiuyanBean.plyDao.paginate(page, 55, select,sqlExceptSelect);
		setAttr("plys", plys);
		render(WEB_INF_URL+"admin/files/listply.jsp");
	}
	
	/**
	 * 删除商品评论
	 * */
	public void delPly(){
		String plycode = getPara("plycode");
		boolean bool = PLiuyanBean.plyDao.deleteById(plycode);
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 删除所有选择的评论
	 * */
	public void delPlyList(){
		final String plycodelist = getPara("plycodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] plycodeArr = plycodelist.split("@");
				for(String plycode: plycodeArr){
					result = PLiuyanBean.plyDao.deleteById(plycode);
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 进入评论详情页面
	 * */
	public void goToDetail(){
		String plycode = getPara("plycode");
		String sql_ = StringUtils.getSql("sql.find.findPlyByCode", plycode);
		Record record = Db.findFirst(sql_);
		setAttr("ply",record);
		render(WEB_INF_URL+"admin/files/plydetail.jsp");
	}
}
